proto.reportSpecResults   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
nc 3
nop 1
1
/**
2
 Jasmine Reporter that outputs test results to the browser console.
3
 Useful for running in a headless environment such as PhantomJs, ZombieJs etc.
4
5
 Usage:
6
 // From your html file that loads jasmine:
7
 jasmine.getEnv().addReporter(new jasmine.ConsoleReporter());
8
 jasmine.getEnv().execute();
9
 */
10
11
(function(jasmine, console) {
12
    if (!jasmine) {
13
        throw "jasmine library isn't loaded!";
14
    }
15
16
    var ANSI = {}
17
    ANSI.color_map = {
18
        "green" : 32,
19
        "red"   : 31
20
    }
21
22
    ANSI.colorize_text = function(text, color) {
23
        var color_code = this.color_map[color];
24
        return "\033[" + color_code + "m" + text + "\033[0m";
25
    }
26
27
    var ConsoleReporter = function() {
28
        if (!console || !console.log) { throw "console isn't present!"; }
29
        this.status = this.statuses.stopped;
30
    };
31
32
    var proto = ConsoleReporter.prototype;
33
    proto.statuses = {
34
        stopped : "stopped",
35
        running : "running",
36
        fail    : "fail",
37
        success : "success"
38
    };
39
40
    proto.reportRunnerStarting = function(runner) {
0 ignored issues
show
Unused Code introduced by
The parameter runner is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
41
        this.status = this.statuses.running;
42
        this.start_time = (new Date()).getTime();
43
        this.executed_specs = 0;
44
        this.passed_specs = 0;
45
        this.log("Starting...");
46
    };
47
48
    proto.reportRunnerResults = function(runner) {
0 ignored issues
show
Unused Code introduced by
The parameter runner is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
49
        var failed = this.executed_specs - this.passed_specs;
50
        var spec_str = this.executed_specs + (this.executed_specs === 1 ? " spec, " : " specs, ");
51
        var fail_str = failed + (failed === 1 ? " failure in " : " failures in ");
52
        var color = (failed > 0)? "red" : "green";
53
        var dur = (new Date()).getTime() - this.start_time;
54
55
        this.log("");
56
        this.log("Finished");
57
        this.log("-----------------");
58
        this.log(spec_str + fail_str + (dur/1000) + "s.", color);
59
60
        this.status = (failed > 0)? this.statuses.fail : this.statuses.success;
61
62
        /* Print something that signals that testing is over so that headless browsers
63
         like PhantomJs know when to terminate. */
64
        this.log("");
65
        this.log("ConsoleReporter finished");
66
    };
67
68
69
    proto.reportSpecStarting = function(spec) {
0 ignored issues
show
Unused Code introduced by
The parameter spec is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
70
        this.executed_specs++;
71
    };
72
73
    proto.reportSpecResults = function(spec) {
74
        if (spec.results().passed()) {
75
            this.passed_specs++;
76
            return;
77
        }
78
79
        var resultText = spec.suite.description + " : " + spec.description;
80
        this.log(resultText, "red");
81
82
        var items = spec.results().getItems()
83
        for (var i = 0; i < items.length; i++) {
84
            var trace = items[i].trace.stack || items[i].trace;
85
            this.log(trace, "red");
86
        }
87
    };
88
89
    proto.reportSuiteResults = function(suite) {
90
        if (!suite.parentSuite) { return; }
91
        var results = suite.results();
92
        var failed = results.totalCount - results.passedCount;
93
        var color = (failed > 0)? "red" : "green";
94
        this.log(suite.getFullName() + ": " + results.passedCount + " of " + results.totalCount + " passed.", color);
95
    };
96
97
    proto.log = function(str, color) {
98
        var text = (color != undefined)? ANSI.colorize_text(str, color) : str;
99
        console.log(text)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
100
    };
101
102
    jasmine.ConsoleReporter = ConsoleReporter;
103
})(jasmine, console);
0 ignored issues
show
Bug introduced by
The variable jasmine seems to be never declared. If this is a global, consider adding a /** global: jasmine */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...